// pages/profile/profile.js
const app = getApp()
var config = require('../../script/config');
var orderApi = require('../../script/order.js');
var publicApi = require('../../script/public.js');
var config = require('../../script/config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: config.baseUrl,
    userinfo: null,
    AllCount: 0,
    ReOrderCount: 0,
    ExpireCount: 0,
    TodayFinishCount: 0,
    TodayCount: 0,
    IncompleteCount: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserInfo()
    // this.getWxMyOrderCount()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    // this.getWxMyOrderCount()
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
  getWxMyOrderCount() {
    if (!this.data.userinfo) return;
    orderApi.GetWxMyOrderCount().then(res => {
      if (res.code === 200) {
        this.setData({
          AllCount: res.data.AllCount,
          ReOrderCount: res.data.ReOrderCount,
          ExpireCount: res.data.ExpireCount,
          TodayCount: res.data.TodayCount,
          TodayFinishCount: res.data.TodayFinishCount,
          IncompleteCount: res.data.IncompleteCount
        })
      }
    });
  },
  getUserInfo: function () {
    try {
      var value = wx.getStorageSync(config.storageKey.userinfo)
      if (value) {
        this.setData({
          userinfo: value
        })
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  toLogin() {
    wx.reLaunch({
      url: "../login/login"
    })
  },
  WxUnBind: function () {
    const that = this;
    wx.showModal({
      title: '提示',
      content: '确定要解除绑定吗？',
      success(resf) {
        if (resf.confirm) {
          publicApi.WxUnBind().then(res => {
            try {
              wx.removeStorageSync(config.storageKey.userinfo);
              wx.removeStorageSync(config.storageKey.token);
              wx.removeStorageSync(config.storageKey.xtoken);
              app.globalData.token = ''
              app.globalData.xtoken = ''
              app.globalData.userInfo = null
            } catch (e) {
              // Do something when catch error
            }
            wx.reLaunch({
              url: "../login/login"
            })
          })
        }
      }
    })
  },
  toPage: function (e) {
    const url = e.currentTarget.dataset.url;
    if (url === 'none') {
      wx.showToast({
        title: '建设中~',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.navigateTo({
        url: "/packageSys/" + url + "/" + url
      })
    }

  },
  call(e) {
    const tel = e.currentTarget.dataset.value;
    wx.makePhoneCall({
      phoneNumber: tel,
    });
    // if (wx.getSystemInfoSync().platform == "android") {
    // 	let main = plus.android.runtimeMainActivity();
    // 	let Intent = plus.android.importClass("android.content.Intent");
    // 	let intent = new Intent(Intent.ACTION_DIAL);
    // 	let Uri = plus.android.importClass("android.net.Uri");
    // 	let data = Uri.parse("tel:" + tel);
    // 	intent.setData(data);
    // 	main.startActivity(intent);
    // 	succ({
    // 		code: 0
    // 	});
    // } else {
    // 	wx.makePhoneCall({
    // 		phoneNumber: tel,
    // 	});
    // }
  }
})